
package com.bible.fire.util;

import java.io.File;
import java.io.FileOutputStream;

import com.bible.fire.local.AppContext;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

/**
 * @author zhangyp
 *
 */
public class ImageUtil {
	
	private static final String TAG = ImageUtil.class.getSimpleName();
	
	/**
	 * 给bitmap加水印
	 * @param src
	 * @param watermark
	 * @return
	 */
	public static Bitmap watermarkBitmap(Bitmap src, Bitmap watermark ){  
        Log.d( TAG, "create a new bitmap" );  
        if( src == null ){  
            return null;  
        }    
        int w = src.getWidth();  
        int h = src.getHeight();  
        int ww = watermark.getWidth();  
        int wh = watermark.getHeight();  
        //创建一个新的和SRC长度宽度一样的位图  
        Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );  
        Canvas cv = new Canvas( newb );  
        //在 0，0坐标开始画入src  
        cv.drawBitmap( src, 0, 0, null );  
        //在src的右下角画入水印    
        cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );
        //save all clip  
        cv.save( Canvas.ALL_SAVE_FLAG );  
        //store  
        cv.restore();//存储  
        return newb;
	}
	
	public static boolean saveBitmapToFile(Bitmap bitmap, String filepath, CompressFormat format, int quality){
		try {  	    	
	        //文件    
	        File file = new File(filepath);  
	        if(!file.getParentFile().exists()){  
	        	file.getParentFile().mkdirs();  
	        }  
	        if (!file.exists()) {  
	            file.createNewFile();  
	        }  	          
	        FileOutputStream fos = null;  
	        fos = new FileOutputStream(file);  
	        if (null != fos) {  
	        	bitmap.compress(format, quality, fos);  
	            fos.flush();  
	            fos.close();
	            return true;
	        }  	  
	    } catch (Exception e) {
	    	Log.e(TAG, "save bitmap error", e); 
	    }  
		return false;
	}
	
	//截图功能(地图除外)
	public static void screenShot(Activity activity){
		//View是你需要截图的View 
		View view = activity.getWindow().getDecorView(); 
		view.setDrawingCacheEnabled(true); 
		view.buildDrawingCache(); 
		Bitmap b1 = view.getDrawingCache(); 
		
		//获取状态栏高度 
		Rect frame = new Rect(); 
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); 
		int statusBarHeight = frame.top; 
		//System.out.println(statusBarHeight); 
		
		//获取屏幕长和高 
		int width = activity.getWindowManager().getDefaultDisplay().getWidth(); 
		int height = activity.getWindowManager().getDefaultDisplay().getHeight(); 
	      
		//去掉标题栏 
		//Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455); 
		Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight); 
		view.destroyDrawingCache(); 
	  
		//3.保存Bitmap
		String filepath = SystemConstant.IMG_PATH_IN_SDCARD + TimeUtil.getSerialNoFromTime() + ".png";
		boolean result = saveBitmapToFile( b, filepath, Bitmap.CompressFormat.PNG, 90 );
		if( result ){
			Toast.makeText(activity, "截屏文件已保存至" + SystemConstant.IMG_PATH_IN_SDCARD + "下", Toast.LENGTH_LONG).show();
		}else{
			Toast.makeText(activity, "截屏文件保存失败", Toast.LENGTH_LONG).show();
		}	    
	}

}
